package maxssina.adapter;

import java.util.List;

import maxssina.activity.R;
import maxssina.common.AsyncImageLoader;
import maxssina.common.Utility;
import maxssina.common.AsyncImageLoader.ImageCallback;
import maxssina.models.WeiBoInfo;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class WeiboAdapter extends BaseAdapter {
	private List<WeiBoInfo> mWeiboList;
	private Context mContext;

	private AsyncImageLoader asyncImageLoader;

	public WeiboAdapter(Context context) {
		mContext = context;
	}

	public void setData(List<WeiBoInfo> datas) {
		mWeiboList = datas;
	}

	public void addData(List<WeiBoInfo> datas) {

		for (int i = datas.size(); i > 0; i--)
			mWeiboList.add(0,datas.get(i - 1));
//		for (WeiBoInfo weiBoInfo : datas) {
//			mWeiboList.add(0,weiBoInfo);
//		}
	}

	@Override
	public int getCount() {
		if (mWeiboList == null)
			return 0;
		return mWeiboList.size();
	}

	@Override
	public Object getItem(int position) {
		return mWeiboList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		asyncImageLoader = new AsyncImageLoader();
		convertView = LayoutInflater.from(mContext).inflate(
				R.layout.listview_weiboinfo, null);
		WeiBoHolder wh = new WeiBoHolder();
		wh.wbicon = (ImageView) convertView.findViewById(R.id.wbicon);
		wh.wbtext = (TextView) convertView.findViewById(R.id.wbtext);
		wh.wbtime = (TextView) convertView.findViewById(R.id.wbtime);
		wh.wbuser = (TextView) convertView.findViewById(R.id.wbuser);
		wh.wbimage = (ImageView) convertView.findViewById(R.id.wbimage);
		WeiBoInfo wb = mWeiboList.get(position);
		if (wb != null) {
			convertView.setTag(wb.getId());
			wh.wbuser.setText(wb.getUserName());
			wh.wbtime.setText(Utility.getTime(wb.getTime()));
			wh.wbtext.setText(wb.getText(), TextView.BufferType.SPANNABLE);
			// textHighlight(wh.wbtext, new char[] { '#' }, new char[] { '#' });
			// textHighlight(wh.wbtext, new char[] { '@' },
			// new char[] { ':', ' ' });
			// textHighlight2(wh.wbtext, "http://", " ");

			if (wb.getHaveImage()) {
				wh.wbimage.setImageResource(R.drawable.icon);
			}
			if (wh.wbicon != null) {
				Drawable cachedImage = asyncImageLoader.loadDrawable(wb
						.getUserIcon(), wh.wbicon, new ImageCallback() {

					@Override
					public void imageLoaded(Drawable imageDrawable,
							ImageView imageView, String imageUrl) {
						imageView.setImageDrawable(imageDrawable);
					}

				});
				if (cachedImage == null) {
					wh.wbicon.setImageResource(R.drawable.usericon);
				} else {
					wh.wbicon.setImageDrawable(cachedImage);
				}
			}
		}

		return convertView;
	}

	public String getLastWeiboId() {
		if (mWeiboList != null && mWeiboList.size() > 0) {
			Utility.Log(mWeiboList.get(0).getWeiboId());
			return mWeiboList.get(0).getWeiboId();
		}
		return "";
	}

	public static class WeiBoHolder {
		ImageView wbicon;
		TextView wbtext;
		TextView wbtime;
		TextView wbuser;
		ImageView wbimage;
	}

	private void textHighlight2(TextView textView, String start, String end) {
		Spannable sp = (Spannable) textView.getText();
		String text = textView.getText().toString();
		int n = 0;
		int s = -1;
		int e = -1;
		while (n < text.length()) {
			s = text.indexOf(start, n);
			if (s != -1) {
				e = text.indexOf(end, s + start.length());
				if (e != -1) {
					e = e + end.length();
				} else {
					e = text.length();
				}
				n = e;
				sp.setSpan(new ForegroundColorSpan(Color.BLUE), s, e,
						Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
				s = e = -1;
			} else {
				n = text.length();
			}
		}
	}

}
